#!/bin/bash

#Configuration Pusher Script w/RANCID
#By Jason Biel
#Version 2


#Set RANCID Variables
RANCIDBASEDIR="/var/network_admin/_rancid"
rCREDENTIALS="$RANCIDBASEDIR/.cloginrc"
rCLOGINPATH="$RANCIDBASEDIR/bin/clogin"
rJLOGINPATH="$RANCIDBASEDIR/bin/jlogin"
rNLOGINPATH="$RANCIDBASEDIR/bin/nlogin"


#Set Config-Pusher Variables
CONFIGPUSHDIR=/var/network_admin/Network-Baselines



pCCHANGELOG="$pRANCIDBASEDIR/push-change-logs/cisco-changelog-`date +%F-%H-%M`.log"
pJCHANGELOG="$pRANCIDBASEDIR/push-change-logs/juniper-changelog-`date +%F-%H-%M`.log"
pNCHANGELOG="$pRANCIDBASEDIR/push-change-logs/netscreen-changelog-`date +%F-%H-%M`.log"



clear
echo "=====[ Acropolis Configuration Pusher ]====="


#Build Location Selection
#echo ""
#echo ""
#echo ""
#echo "Step 1"
#echo "=====[ Choose Site Location ]====="
#echo "1.) DECC St. Louis"
#echo "2.) DECC Robins"
#echo "3.) NAS Pensacola"
#echo "4.) EECOS"
#echo "5.) All"
#echo ""
#echo -n ">> "
#read pSITELOC

#Build Device Vendor Selection
echo "=====[ Configuration Push Script ]====="
echo ""
echo ""
echo ""
echo "Step 1"
echo "=====[ Choose Device OS ]====="
echo "1.) Cisco IOS"
echo "2.) Juniper JUNOS"
echo ""
echo -n ">> "
read pDEVICEOS

#


#Build Device-List Menu and Arrary
echo ""
echo ""
echo "Step 2"
echo "Please choose the device list:"

pDVLISTCNT=1
for dvlist in `ls $pDEVICELISTPATH | grep .dl`
  do
   echo "$pDVLISTCNT.) $dvlist"; device_array[$pDVLISTCNT]="$dvlist"
   ((pDVLISTCNT++))
done

echo "--------------------------------------"
echo -n ">> "
read pDEVICELIST

if [ -f $pDEVICELISTPATH${device_array[$pDEVICELIST]} ]
 then
  echo ""
  echo "Device List = ${device_array[$pDEVICELIST]} (confirmed)"
 else
  echo ""
  echo "Device list = ${device_array[$pDEVICELIST]} (does not exist!)"
  echo "Aborting..."
  echo ""
   exit
fi

#Build Change Script Menu and Array
echo ""
echo "Please choose the change script:"

pCSCRIPTCNT=1
for cslist in `ls $pCHANGESCRIPTPATH | grep .cs`
  do
   echo "$pCSCRIPTCNT.) $cslist"; change_array[$pCSCRIPTCNT]="$cslist"
   ((pCSCRIPTCNT++))
done


echo "-----------------------------------"
echo -n ">> "
read pCHANGESCRIPT

if [ -f $pCHANGESCRIPTPATH${change_array[$pCHANGESCRIPT]} ]
 then
  echo ""
  echo "Change Script = ${change_array[$pCHANGESCRIPT]} (confirmed)"
  echo ""
 else
  echo "Device list = ${change_array[$pCHANGESCRIPT]} (does not exist!)"
  echo "Aborting..."
  echo ""
   exit
fi

#Display changes for devices
echo "-- Proposed Changes For Below Devices --"
echo "`cat $pCHANGESCRIPTPATH${change_array[$pCHANGESCRIPT]}`"
echo "-- Proposed Changes For Below Devices --"
echo ""
echo "-- Selected Devices --"
echo "`cat $pDEVICELISTPATH${device_array[$pDEVICELIST]}`"
echo ""
echo "Are you sure you want to proceed?  If so, type \"yes\":"
echo -n ">> "
read pAREYOUSURE


if [ $pAREYOUSURE != "yes" ]
 then
  echo ""
  echo "Aborting..."
  echo ""
   exit
 else
  echo ""
  echo "Implementing Changes..."
  echo ""
fi



#Setup RANCID Command
if [ $pDEVICEOS == "1" ];
 then
  pCOMMAND="$pCLOGINPATH"; pLOGGING="$pCCHANGELOG"
 else
  pCOMMAND="$pJLOGINPATH"; pLOGGING="$pJCHANGELOG"
fi



for pDEVICE in `cat $pDEVICELISTPATH${device_array[$pDEVICELIST]}`
 do
  echo "===[ $pDEVICE ]==="
  echo "" >> $pLOGGING
  echo "===[ $pDEVICE ]===" >> $pLOGGING
  echo "" >> $pLOGGING
  pOUTPUT=`$pCOMMAND -f $pCREDENTIALS -x $pCHANGESCRIPTPATH${change_array[$pCHANGESCRIPT]} $pDEVICE`
  echo "$pOUTPUT" >> $pLOGGING
done

echo "" >> $pLOGGING
echo "===[ Completed ]===" >> $pLOGGING
echo "===[ Completed - See log for details $pLOGGING ]==="
echo "===[ See log for details $pLOGGING ]==="
echo "" >> $pLOGGING
